using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace MyMedia.iPod.Music
{
	public class Root : Node
	{
		Mhbd m_mhbd;

		//protected override int Size
		//{
		//    get { return m_mhbd.HeaderLength; }
		//}

		protected override void ReadImpl(BinaryReader reader)
		{
			m_mhbd = new Mhbd();
			m_mhbd.Read(reader);

			for (int l = 0; l < m_mhbd.ChildCount; l++)
			{
				DataSet dataSet = new DataSet();
				AddChild(dataSet);
				dataSet.Read(reader);
				
				if (dataSet.Type == 1)
				{
					TrackList list = new TrackList();
					dataSet.AddChild(list);
					list.Read(reader);
				}
				else if (dataSet.Type == 2 || dataSet.Type == 3)
				{
					PlaylistList playlistList = new PlaylistList();
					dataSet.AddChild(playlistList);
					playlistList.Read(reader);
				}
				else if (dataSet.Type == 4)
				{
					AlbumList list = new AlbumList();
					dataSet.AddChild(list);
					list.Read(reader);
				}
				else
				{
					byte[] identifier = reader.ReadBytes(4);
					int headerLength = reader.ReadInt32();
					reader.ReadBytes(headerLength - 8);
				}
			}
		}

		protected override void WriteImpl(BinaryWriter writer)
		{
			m_mhbd.Write(writer);
		}

		protected override void SetSize(int size)
		{
			m_mhbd.TotalLength = size;
		}
	}
}
